import { Component, OnInit } from '@angular/core';
import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { BladeData } from "../../../../shared/blade/blade-data";
import { Blade } from "../../../../shared/blade/blade.model";
import { Command } from "../../../../shared/toolbar.service";
import { DialogResult } from "../../../../shared/dialog.service";
import { Observable } from "rxjs/Observable";
import { Setofkey } from "../../../../entity/setofkey.model";
import { Farm } from "../../../../entity/farm.model";
import { User } from '../../../../entity/user.model';
import { AlarmCategory } from "../../../../entity/alarm-category.model";
import { SetofkeyService } from "../../../../service/setofkey.service";

import { UserfarmAlarmconfigEditComponent } from "../userfarm-alarmconfig-edit/userfarm-alarmconfig-edit.component"
import { UserFarmAlarmconfig } from "../../../../entity/user_farm_alarmconfig.model";
import { UserFarmAlarmconfigService } from "../../../../service/user-farm-alarmconfig.service"
import { FarmService } from '../../../../service/farm.service';
import { AlarmCategoryService } from '../../../../service/alarm-category.service';

@Component({
	selector: 'app-userfarm-alarmconfig-list',
	templateUrl: './userfarm-alarmconfig-list.component.html',
	styleUrls: ['./userfarm-alarmconfig-list.component.css']
})
export class UserfarmAlarmconfigListComponent extends BladeContainerBase<UserFarmAlarmconfig> {

	public userfarmAlarmconfigData: UserFarmAlarmconfig[] = [];
	public original: UserFarmAlarmconfig[] = [];

	public AlarmCategorys: AlarmCategory[] = [];

	public farms: Farm[] = [];
	constructor(
		private userFarmAlarmconfigService: UserFarmAlarmconfigService,
		private farmService: FarmService,
		private alarmCategoryService: AlarmCategoryService
	) { super() }

	ngOnInit(): void {
		this.commands = [
			new Command("edit", "配置", "fa fa-edit", "setofkey:update", blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
		];
		super.ngOnInit();
	}

	initData(): any | Observable<any> | Promise<any> {
		let userFarmAlarmGet = this.userFarmAlarmconfigService.getUserFarmAlarmconfigsByUserId((this.parentEntity as User).userId);
		// let farmGet = this.farmService.getFarmsWithTotalTurbineByUserId(this.parentEntity.userId);
		let farmGet = this.farmService.getFarms();
		let alarmCategoryGet = this.alarmCategoryService.getAlarmCategorys();
		Promise.all([userFarmAlarmGet, farmGet, alarmCategoryGet])
			.then(res => {
		
				if (res[0]) {
					this.original = [...res[0]];
				}
				// this.userfarmAlarmconfigData = [...this.original];
				if (res[1]) {
					this.farms = [...res[1]];

				};
				if (res[1]) {
					this.AlarmCategorys = [...res[2]];
				};
				this.userfarmAlarmconfigData = [...this.original];
				if (this.farms && this.AlarmCategorys) {
			
					this.userfarmAlarmconfigData.forEach(config => {
						let index = this.farms.findIndex(o => o.farmId === config.farmId);
				
				
						let index2 = this.AlarmCategorys.findIndex(o => o.alarmCategoryId === config.alarmCategoryId);
						if(index !== -1){
							config.farmName = this.farms[index].farmName;
						}
												
						config.alarmCategoryName = this.AlarmCategorys[index2].alarmCategoryName;
					});
		
				}
				
			})
			.catch(error => this.bladeData.error = error)
			.then(res => this.bladeData.isLoading = false);
	}

	exeEdit(blend: Blade): void {
		if (this.currentEntity != undefined) {
			let data: BladeData = new BladeData();
			data.id = "UserfarmAlarmconfigEdit";
			data.title = this.currentEntity.farmName + '报警配置';
			let userfarmAlarmconfigBlade = this.bladeService.createBlade(UserfarmAlarmconfigEditComponent, data, false, this.currentEntity, this.userfarmAlarmconfigData);
			this.currentBlade.bladeService.showBlade(userfarmAlarmconfigBlade, this.currentBlade);
		}
	}
	canExeEdit(blend?: Blade): boolean {
		return this.currentEntity != null;
	}

	onSeletedChange(userFarmAlarmconfig: UserFarmAlarmconfig) {
		if (this.currentBlade.childrenCanDeactivate()) {
			this.currentEntity = userFarmAlarmconfig;
		}
	}

}



// WEBPACK FOOTER //
// ./src/app/module/basic/user-farm-alarmconfig/userfarm-alarmconfig-list/userfarm-alarmconfig-list.component.ts